﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
using YArchitech.LIB;

namespace SystemDiagramNew
{
	public partial class LoadingForm : System.Windows.Forms.Form, IProceeding
	{
		public LoadingForm()
		{
			this.InitializeComponent();
			YJKProcessTools hyprocessTools = new YJKProcessTools();
			hyprocessTools.ShowInTaskbarChange(this);
			this.loadingCircle1.Active = true;
		}

		public void Proceeding()
		{
			int tickCount = Environment.TickCount;
			if (tickCount - this.prevTick < 100)
			{
				return;
			}
			Application.DoEvents();
			this.prevTick = tickCount;
		}

		public void Report(int progress, string message)
		{
			this.Proceeding();
			if (!string.IsNullOrEmpty(message))
			{
				this.tbMsg.Text = message;
			}
		}

		public void Show(IntPtr ownerHandle)
		{
			this.prevTick = Environment.TickCount;
			Control control = Control.FromHandle(ownerHandle);
			IWin32Window owner;
			if (control == null)
			{
				owner = new WindowWrapper(ownerHandle);
				base.StartPosition = FormStartPosition.CenterScreen;
			}
			else
			{
				owner = control;
				base.StartPosition = FormStartPosition.CenterParent;
			}
			base.Show(owner);
		}

		public void ShowDialog(Action action)
		{
			base.Shown += delegate(object _, EventArgs args)
			{
				Task.Factory.StartNew(delegate
				{
					action();
					this.Close();
				});
			};
			base.ShowDialog();
		}

		private int prevTick;
	}
}
